package com.nativetalk.service.four.picture;

import com.aliyun.openservices.oss.model.ObjectMetadata;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.TdNtTeacherPicture;
import com.nativetalk.bean.picture.TdNtTeacherPictureId;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.oss.OssUtil;
import com.nativetalk.dao.TdNtTeacherPictureDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by lizhun on 2016/11/1.
 */
@Service
public class FourPictureServiceImpl implements FourPictureService {
    private static Logger logger = Logger.getLogger(FourPictureServiceImpl.class);
    @Resource
    private TdNtTeacherPictureDao tdNtTeacherPictureDao;
    @Override
    public RetInfo findPicturesByTeaId(int tea_id) {
        String logInfo = this.getClass().getName() + ":findPicturesByTeaId:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {

            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取列表成功");
            retInfo.setObj(tdNtTeacherPictureDao.findPictureByTeaId(tea_id));



        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo addPicture(int tea_id, CommonsMultipartFile[] pictures) {
        String logInfo = this.getClass().getName() + ":addPicture:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();

        try {
            Integer pictures_size = tdNtTeacherPictureDao.findPictureByTeaIdSize(tea_id);
            /**
             * 查找直播图片数量
             */
            if (pictures.length > (30-pictures_size)) {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setObj("No more than 30 pictures");
            } else {

                /**
                 * 上传图片 添加图片
                 */
                for(int i = 0;i<pictures.length;i++){

                    if(!pictures[i].isEmpty()){
                        ObjectMetadata objectMetadata_picture = new ObjectMetadata();
                        objectMetadata_picture.setContentLength(pictures[i].getSize());
                        String picture = Constants.FILE + "/" + Constants.FILEIMG + "/image/teacher/live/" + tea_id
                                +"/picture/"
                                +new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(System.currentTimeMillis()))
                                +i+pictures[i].getOriginalFilename().substring(pictures[i].getOriginalFilename().lastIndexOf("."));
                        OssUtil.uploadInputStream(pictures[i].getInputStream(), objectMetadata_picture, picture);
                        TdNtTeacherPicture tdNtTeacherPicture = new TdNtTeacherPicture();
                        tdNtTeacherPicture.setPic_url(picture);
                        tdNtTeacherPicture.setIs_valid(0);
                        tdNtTeacherPicture.setTea_id(tea_id);
                        tdNtTeacherPictureDao.insertPicture(tdNtTeacherPicture);
                    }
                }
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setObj("添加成功");
            }

        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo deletePicture(int tea_id, TdNtTeacherPictureId tdNtTeacherPictureId) {
        String logInfo = this.getClass().getName() + ":deletePicture:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();

        try {
            if (tdNtTeacherPictureId.getTea_pic_ids().length > 0) {
                for (int i = 0; i < tdNtTeacherPictureId.getTea_pic_ids().length; i++) {

                    TdNtTeacherPicture tdNtTeacherPicture = new TdNtTeacherPicture();
                    tdNtTeacherPicture.setTea_pic_id(tdNtTeacherPictureId.getTea_pic_ids()[i]);
                    tdNtTeacherPicture.setIs_valid(1);
                    tdNtTeacherPictureDao.updatePicture(tdNtTeacherPicture);
                }
            }

            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("成功");



        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
